<template>
    <el-scrollbar height="610px">
    <div class="user-info-edit">
        <!-- 顶部导航栏 -->
        <div class="header">
            <div class="header-left">
                <button class="back-button" @click="">
                </button>
            </div>
            <div class="header-title">个人信息</div>
            <div class="header-right"></div>
        </div>

        <!-- 头像区域 -->
        <div class="avatar-section">
            <img :src="'http://localhost:8081' + user.yhtx" alt="用户头像" class="avatar">
        </div>

        <el-form-item label="头像上传" prop="picture">
            <el-upload drag style="width:100%"
                       action="http://localhost:8081/show"
                       :on-success="handleFileUploadSuccess"
            >
                <el-icon class="el-icon--upload"><upload-filled/></el-icon>
                <div class="el-upload__text">将文档拖拽到此处，或 <em>点击上传</em></div>
                <template #tip>
                    <div class="el-upload__tip">支持 jpg/png 格式， 大小不超过 5M</div>
                </template>
            </el-upload>
        </el-form-item>

        <!-- 信息表单 -->
        <div class="info-form">
            <div class="form-item">
                <div class="item-label">昵称</div>
                <div class="item-value">
                    <input type="text" v-model="user.yhm" placeholder="请输入昵称">
                </div>
            </div>

            <div class="form-item">
                <div class="item-label">账号</div>
                <div class="item-value">
                    <span>{{user.id}}</span>
                </div>
            </div>

            <div class="form-item">
                <div class="item-label">性别</div>
                <div class="item-value">
                    <el-form-item>
                        <el-select v-model="user.sex" placeholder="请选择性别" style="width: 150px">
                            <el-option label="女" value="0" />
                            <el-option label="男" value="1" />
                        </el-select>
                    </el-form-item>
                </div>
            </div>

            <div class="form-item">
                <div class="item-label">年龄</div>
                <div class="item-value">
                    <input type="text" v-model="user.age" placeholder="填写年龄">
                </div>
            </div>
            <div class="form-item">
                <div class="item-label">密码修改</div>
                <div class="item-value">
                    <input type="text" v-model="user.pwd" placeholder="输入新密码">
                </div>
            </div>
        </div>
        <el-row justify="center">
            <el-col :span="10">
                <el-button type="primary" style="width: 80%;height: 50px" @click="save">保存</el-button>
            </el-col>
        </el-row>
    </div>
    </el-scrollbar>
</template>

<script setup lang="ts">
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "../../../router.ts";

//--------------------------上传文件-----------------------
const handleFileUploadSuccess = (fileName) => {
    if (fileName != null || fileName != '') {
        user.value.yhtx= fileName;
        ElMessage({type: 'success',message:'文件上传成功'})
    }else{
        ElMessage({type: 'error',message:'文件上传失败'})
    }
}
//--------------------------获取用户信息-----------------------
const user=ref({
    id:'',
    yhm:'',
    pwd:'',
    yhtx:'',
    sjh:'',
    age:'',
    sex:''
});
const acquireUsers=async ()=>{
    const {data} = await axios.get('/api/user1',{params:user.value});
    if(data.length==0)
        router.push('/login');
    user.value=data;
}
//-----------------------------保存----------------------
const emit = defineEmits(['userUpdated'])
const save=async ()=>{
    const {data} = await axios.put('/api/user/'+user.value.id,user.value);
    if(data!=""){
        ElMessage.success('保存成功!');
    }else{
        ElMessage.error('保存失败!');
    }
    emit('userUpdated') // 发出事件通知父组件
}

//--------------------------页面自动刷新-----------------------
onMounted(()=>{
    acquireUsers();
})
</script>

<style scoped>

.user-info-edit {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: #f5f5f5;
    min-height: 100vh;
}

/* 顶部导航栏样式 */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background-color: #fff;
    border-bottom: 1px solid #eee;
}

.header-title {
    font-size: 18px;
    font-weight: 500;
}

.back-button {
    border: none;
    background: none;
    padding: 8px;
}

/* 头像区域样式 */
.avatar-section {
    padding: 20px 0;
    background-color: #fff;
    margin-bottom: 10px;
    text-align: center;
}

.avatar-container {
    position: relative;
    display: inline-block;
}

.avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background-color: #ffd700;
}

.camera-icon {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 24px;
    height: 24px;
    background-color: #ffd700;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-camera {
    display: inline-block;
    width: 14px;
    height: 14px;
    background-color: #fff;
    border-radius: 50%;
}

/* 信息表单样式 */
.info-form {
    background-color: #fff;
    padding: 0 16px;
}

.form-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 0;
    border-bottom: 1px solid #eee;
}

.form-item:last-child {
    border-bottom: none;
}

.item-label {
    font-size: 16px;
    color: #333;
}

.item-value {
    display: flex;
    align-items: center;
}

.item-value input {
    border: none;
    text-align: right;
    font-size: 16px;
    color: #999;
    padding-right: 8px;
}

.item-value input:focus {
    outline: none;
    color: #333;
}

</style>